﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.IO
Imports System.Linq
Imports System.Web
Imports System.Xml.Serialization

Namespace roncliProductions.LibWowArmory.Character.Statistics.Category

    Public Class CharacterStatisticsCategory
        Inherits WowArmoryData

#Region "Declarations"

        Private cscCategory As New Schema.category

#End Region

        ''' <summary>
        ''' The realm the character is on
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Realm As String

        ''' <summary>
        ''' The name of the character
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Name As String

        ''' <summary>
        ''' The category ID of the statistics to lookup
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property CategoryID As Integer

        ''' <summary>
        ''' The URL that will be used to retrieve the character's statistics from the Armory
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL As String
            Get
                Return String.Format(CultureInfo.InvariantCulture, "http://www.wowarmory.com/character-statistics.xml?r={0}&n={1}&c={2}", Realm, Name, CategoryID)
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request does not require login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' The statistics for this category
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Statistics As Category

        ''' <summary>
        ''' A constructor to load a character's statistics by realm, character name, and category ID
        ''' </summary>
        ''' <param name="strRealm"></param>
        ''' <param name="strName"></param>
        ''' <param name="intCategoryID"></param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strRealm As String, ByVal strName As String, ByVal intCategoryID As Integer)
            Realm = strRealm
            Name = strName
            CategoryID = intCategoryID
            Load()
        End Sub

        ''' <summary>
        ''' Loads the character's statistics from the armory
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            Dim strKey As String = String.Format(CultureInfo.InvariantCulture, "LibWowArmory.CharacterStatisticsCategory.{0}.{1}.{2}", Realm, Name, CategoryID)
            If Cache.Cache(strKey) Is Nothing Then
                MyBase.Retrieve()

                Dim xsSerializer As New XmlSerializer(cscCategory.GetType)

                Using srXML As New StringReader(MyBase.Data)
                    cscCategory = CType(xsSerializer.Deserialize(srXML), Schema.category)
                End Using

                Cache.Cache.Insert(strKey, cscCategory, Nothing, Date.UtcNow.AddDays(1), Nothing, Caching.CacheItemPriority.Normal, Nothing)
            Else
                cscCategory = CType(Cache.Cache(strKey), Schema.category)
            End If

            If cscCategory Is Nothing Then
                Throw New ArgumentException("The character you are trying to retrieve statistics for does not exist.")
            End If
            If cscCategory.category Is Nothing Then
                Throw New ArgumentException("The character you are trying to retrieve statistics for does not exist.")
            End If

            ProcessCategory(Statistics, cscCategory)
        End Sub

        Private Sub ProcessCategory(ByRef cCategoryToProcess As Category, ByRef cscCategoryFromXML As Schema.category)
            For Each cscStatistic As Schema.statistic In cscCategoryFromXML.statistic
                cCategoryToProcess.Statistics.Add(New Statistic(cscStatistic.id, cscStatistic.highest, cscStatistic.name, cscStatistic.quantity))
            Next
            For Each cscSubcategory As Schema.category In cscCategoryFromXML.category
                Dim cSubcategory As New Category
                ProcessCategory(cSubcategory, cscSubcategory)
                cCategoryToProcess.Subcategories.Add(cSubcategory)
            Next
        End Sub

    End Class

End Namespace